﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SalaDeEnsayo.ApplicationService.Model.Entity;

namespace SalaDeEnsayo.ApplicationService
{
    public class ReservaService : BaseService
    {
        #region [ Metodos Publicos ]

        public List<ReservaDTO> ObtenerPorSalaPorDia(DateTime fecha, int sala)
        {
            List<ReservaDTO> listaReserva = new List<ReservaDTO>();
            using (var dr = this.SalaDeEnsayoDB.ExecuteReader("ReservasObtenerPorSalaPorDia", fecha, sala))
            {
                while (dr.Read())
                {
                    listaReserva.Add(GetEntityDTO(dr));
                }
            }
            return listaReserva;
        }

        public Reserva ObtenerPorDiaHora(DateTime hora)
        {
            Reserva reserva = new Reserva();
            using (var dr = this.SalaDeEnsayoDB.ExecuteReader("ReservaObtenerPorDiaHora", hora))
            {
                if (dr.Read())
                {
                    reserva = GetEntityReserva(dr);
                }
            }
            return reserva;
        }

        public int AgerarModificarReserva(Reserva reserva)
        {
            try
            {
                if (reserva.Id == 0)
                {
                    Object[] param = new Object[6];
                    param[0] = reserva.IdGrupo;
                    param[1] = reserva.IdSala;
                    param[2] = reserva.HorarioEntrada;
                    param[3] = reserva.HorarioSalida;
                    param[4] = reserva.Senia;
                    param[5] = reserva.Saldo;
                    //param[5] = reserva.IdEstado;
                    return this.SalaDeEnsayoDB.ExecuteNonQuery("ReservasAgregar", param);
                }
                else
                {
                    Object[] param1 = new Object[8];
                    param1[0] = reserva.Id;
                    param1[1] = reserva.IdGrupo;
                    param1[2] = reserva.IdSala;
                    param1[3] = reserva.HorarioEntrada;
                    param1[4] = reserva.HorarioSalida;
                    param1[5] = reserva.Senia;
                    param1[6] = reserva.Saldo;
                    param1[7] = reserva.IdEstado;
                    return this.SalaDeEnsayoDB.ExecuteNonQuery("ReservasModifcar", param1);
                }
            }
            catch (Exception e)
            {
                return 0;
            }
        }

        public int BorrarReserva(Reserva reserva)
        {
            try
            {
                Object[] param1 = new Object[1];
                param1[0] = reserva.Id;

                return this.SalaDeEnsayoDB.ExecuteNonQuery("ReservasEliminar", param1);

            }
            catch
            {
                return 0;
            }
        }
        public Boolean PuedeInsertar(DateTime hora_entrada, DateTime hora_salida, long id_sala, long id_reserva)
        {
            Int64 puede;
            Object[] param1 = new Object[4];
            param1[0] = hora_entrada.ToString().Replace(".", "");
            param1[1] = hora_salida.ToString();
            param1[2] = id_sala;
            param1[3] = id_reserva;



            puede = Convert.ToInt64(this.SalaDeEnsayoDB.ExecuteScalar("ReservasPuedeInsertar", param1));

            if (puede == 1)
            {
                return true;
            }

            return false;
        }

        public int EliminarReservasGrupoPost(long id_grupo)
        {
            try
            {
                return this.SalaDeEnsayoDB.ExecuteNonQuery("ReservasBorrarporGruposPost", id_grupo);
            }
            catch
            {
                return 0;
            }

        }

        public int EliminarReservasGrupoPre(long id_grupo)
        {
            try
            {
                return this.SalaDeEnsayoDB.ExecuteNonQuery("ReservasBorrarporGruposPre", id_grupo);
            }
            catch
            {
                return 0;
            }

        }

        public int EliminarReservasSala(long id_sala)
        {
            try
            {
                return this.SalaDeEnsayoDB.ExecuteNonQuery("ReservasBorrarporSala", id_sala);
            }
            catch
            {
                return 0;
            }

        }

        #endregion

        #region [ Metodos privados ]

        private Reserva GetEntityReserva(System.Data.IDataReader dr)
        {
            Reserva reserva = new Reserva();
            reserva.Id = dr["id_reserva"] is DBNull ? 0 : (long)dr["id_reserva"];
            reserva.IdGrupo = dr["id_grupo"] is DBNull ? 0 : (long)dr["id_grupo"];
            reserva.IdSala = dr["id_sala"] is DBNull ? 0 : (int)dr["id_sala"];
            reserva.HorarioEntrada = dr["horario_entrada"] is DBNull ? DateTime.MinValue : (DateTime)dr["horario_entrada"];
            reserva.HorarioSalida = dr["horario_salida"] is DBNull ? DateTime.MinValue : (DateTime)dr["horario_salida"];
            reserva.Senia = dr["senia"] is DBNull ? 0 : (Decimal)dr["senia"];
            reserva.Saldo = dr["saldo"] is DBNull ? 0 : (Decimal)dr["saldo"];
            reserva.IdEstado = dr["id_estado"] is DBNull ? 0 : (int)dr["id_estado"];
            return reserva;
        }

        private ReservaDTO GetEntityDTO(System.Data.IDataReader dr)
        {
            return new ReservaDTO(dr["hora"] is DBNull ? 0 : (int)dr["hora"],
                                  dr["grupo"] is DBNull ? string.Empty : (string)dr["grupo"]);
        }

        //private Sala GetEntitySala(System.Data.IDataReader dr)
        //{
        //    return new Sala(dr["id_sala"] is DBNull ? 0 : (int)dr["id_sala"],
        //                    dr["nombre"] is DBNull ? string.Empty : (string)dr["nombre"],
        //                    dr["descripcion"] is DBNull ? string.Empty : (string)dr["descripcion"]);
        //}
        #endregion
    }

    public class ReservaDTO
    {
        public ReservaDTO(int hora, string grupo)
        {
            this.Hora = hora;
            this.Grupo = grupo;
        }
        public int Hora { get; set; }
        public string Grupo { get; set; }
        public bool Existe
        {
            get { return Grupo != string.Empty; }
        }
        public bool NoExiste
        {
            get { return !Existe; }
        }
    }

    //public class Sala
    //{
    //    public Sala(int id, string nombre, string descripcion)
    //    {
    //        this.Id = id;
    //        this.Nombre = nombre;
    //        this.Descripcion = descripcion;
    //    }
    //    public int Id { get; set; }
    //    public string Nombre { get; set; }
    //    public string Descripcion { get; set; }
    //}
}
